clear all
*cap log close
set more off


* create summary stats table -------------------------------------------------------

* create samples --------------
use "${data}/out/4-main", clear
gen sample_all   = 1
gen sample_nine = (speeddiff == 9)
gen sample_tenplus  = (speeddiff >=10)

* create covariates -------------
gen race_w = (race=="W")
label var race_w "Race = White"
replace zipincome = zipincome*1000
label var veh_price "Vehicle Value"
label var contest "Contest in Court"
label var cite_py1 "Citation Past Year"
label var speed_py1 "Speeding Past Year"
label var other_py1 "Other Past Year"
label var crashany_py1 "Crash Past Year"

* grouped covariate lists --------------
global COVdem = "female age age_miss race_w race_b race_h race_o race_u"
global COVsoc = "zipincome zipincome_miss veh_price veh_miss"
global COVhis = "priorprison cite_py1 speed_py1 other_py1 crashany_py1"
global COVoff = "speeddiff fine contest"


* write summary stats as dataset -----------
gen head = ""
gen var  = ""
gen tm1  = .
gen tm2  = .
gen tm3  = . 
local k = 1

replace head = "Panel A: Demographics" if _n == `k'
foreach x in $COVdem {
	local lab: variable label `x'
	replace var = "`lab'" if _n == `k'
	summ `x' if sample_all == 1
	replace tm1 = r(mean) if _n == `k'

	summ `x' if sample_nine == 1
	replace tm2 = r(mean) if _n == `k'

	summ `x' if sample_tenplus == 1
	replace tm3 = r(mean) if _n == `k'

	local ++k
}

replace head = "Panel B: Socioeconomic Status" if _n == `k'
foreach x in $COVsoc {
	local lab: variable label `x'
	replace var = "`lab'" if _n == `k'
	summ `x' if sample_all == 1
	replace tm1 = r(mean) if _n == `k'

	summ `x' if sample_nine == 1
	replace tm2 = r(mean) if _n == `k'

	summ `x' if sample_tenplus == 1
	replace tm3 = r(mean) if _n == `k'

	local ++k
}

replace head = "Panel C: Offending History" if _n == `k'
foreach x in $COVhis {
	local lab: variable label `x'
	replace var = "`lab'" if _n == `k'
	summ `x' if sample_all == 1
	replace tm1 = r(mean) if _n == `k'

	summ `x' if sample_nine == 1
	replace tm2 = r(mean) if _n == `k'

	summ `x' if sample_tenplus == 1
	replace tm3 = r(mean) if _n == `k'

	local ++k	
}

replace head = "Panel D: Offense Characteristics" if _n == `k'
foreach x in $COVoff {
	local lab: variable label `x'
	replace var = "`lab'" if _n == `k'
	summ `x' if sample_all == 1
	replace tm1 = r(mean) if _n == `k'

	summ `x' if sample_nine == 1
	replace tm2 = r(mean) if _n == `k'

	summ `x' if sample_tenplus == 1
	replace tm3 = r(mean) if _n == `k'

	local ++k	
}

forval i = 1/3 {
	gen m`i' = string(tm`i',"%4.3f")
	replace m`i' = string(tm`i',"%4.2f") if tm`i'>1
	replace m`i' = string(tm`i',"%5.0f") if tm`i'>1000
	replace m`i' = string(tm`i',"%5.4f") if tm`i'<0.001
}


* post to LaTeX table ---------------------------------
capture erase "${out}/main/table_summary.tex"
file open fh using "${out}/main/table_summary.tex", write replace
file write fh ///	
	"&\multicolumn{1}{c}{} && \multicolumn{2}{c}{By Fines}\\\cline{4-5}" _n /// 
	"&&\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)} \\" _n ///
	"&&\multicolumn{1}{c}{All}&\multicolumn{1}{c}{Discounted}&\multicolumn{1}{c}{Harsh} \\" _n ///
	"\hline" _n

local k = 1	
local head = head[`k']
file write fh ///
	"\multicolumn{5}{l}{\underline{\textit{`head'}}}  \\ [0.5ex]"  _n ///
	
foreach v in $COVdem { 
	local label = var[`k']
	local m1    = m1[`k']
	local m2    = m2[`k']
	local m3    = m3[`k']
	file write fh "`label' && `m1' & `m2' & `m3' \\ [1ex]" _n
	local ++k	
}

local head = head[`k']
file write fh ///
	"\multicolumn{5}{l}{\underline{\textit{`head'}}}  \\ [0.5ex]"  _n ///
	
foreach v in $COVsoc { 
	local label = var[`k']
	local m1    = m1[`k']
	local m2    = m2[`k']
	local m3    = m3[`k']
	file write fh "`label' && `m1' & `m2' & `m3' \\ [1ex]" _n
	local ++k	
}
	
local head = head[`k']
file write fh ///
	"\multicolumn{5}{l}{\underline{\textit{`head'}}}  \\ [0.5ex]"  _n ///
	
foreach v in $COVhis { 
	local label = var[`k']
	local m1    = m1[`k']
	local m2    = m2[`k']
	local m3    = m3[`k']
	file write fh "`label' && `m1' & `m2' & `m3' \\ [1ex]" _n
	local ++k	
}

local head = head[`k']
file write fh ///
	"\multicolumn{5}{l}{\underline{\textit{`head'}}}  \\ [0.5ex]"  _n ///
	
foreach v in $COVoff { 
	local label = var[`k']
	local m1    = m1[`k']
	local m2    = m2[`k']
	local m3    = m3[`k']
	file write fh "`label' && `m1' & `m2' & `m3' \\ [1ex]" _n
	local ++k
}

local k=1
foreach s in all nine tenplus {
	count if sample_`s'==1
	local m`k' = string(r(N), "%10.0fc")
	local ++k
}
file write fh "\hline Observations && `m1' & `m2' & `m3'\\ " _n
file write fh ///
"\bottomrule  " _n 

file close fh
macro drop fh
* ----------------------------------------------------------------------------------



